<template>
	<div class="custom-list-tab-wrap">
		<div class="heade-wrapper">
			<a-input-search
				:placeholder="placeText"
				v-model.trim="searchValue"
				@search="searchHandle"
			/>
		</div>
		<div class="content-wrapper">
			<scroll-list
				:panel="tabMainInfo.name"
				:command="command"
				:firstLoadSearch="firstLoadSearch"
				:iconType="setIconType()"
				ref="scrollList"
				:listDataHandler="onSetListData"
				:checkAllShow="checkAllShow"
				@listSimulation="listSimulation"
			/>
		</div>
	</div>
</template>

<script>
import scrollList from '@/components/scrollList';
import chooseList from '@/components/chooseList';

import {EntityTypes} from '@/utils/orgConstants';
import { getSelectKey, isSubDeptType } from '@/utils/utils';

export default {
	name: 'OrgRecent',
	components: { scrollList, chooseList },
	inject: ['app'],
	props: {
		/** 【必填参数】*/
		/** 页签的主数据 */
		tabMainInfo: {
			type: Object, default: () => {
			}
		}
	},
	data() {
		return {
			searchValue: '',
			otherProps: ['deptName'],
			titleProps: { code: '编号' },
			firstLoadSearch: false,

			serachParams: {}
		};
	},
	watch: {},
	computed: {
		orgSelect() {
			return this.app().orgSelect;
		},
		command() {
			console.log('commaond', this.tabMainInfo.extInfo.displayType);
			return this.tabMainInfo.extInfo.displayType;
		},
		runMode() {
			return this.app().runMode;
		},
		placeText() {
			return this.onSetPlaceText();
		},
		/**
		 * 自定义列表下，单位、部门、岗位、角色、职务级别、组不能有全选按钮
		*/
		checkAllShow() {
			const type = this.tabMainInfo.extInfo.selectType && this.tabMainInfo.extInfo.selectType[0];
			if (type === EntityTypes.Account || type === EntityTypes.Department || type === EntityTypes.Post || type === EntityTypes.Role || type === EntityTypes.Level || type === EntityTypes.Team || type === EntityTypes.OrgTeam) {
				return false;
			} else {
				return true;
			}
		}
	},

	methods: {
		onSearchData(data, orgParams) {
			this.firstLoadSearch = true;
			this.$refs.scrollList.initProps();
			this.$refs.scrollList.search(data, orgParams);
		},
		searchHandle(val) {
			this.onSearchData({ keyword: val, ...this.serachParams });
		},
		listSimulation(time) {
			if (this.runMode === 'SIMULATION' && time) {
				window.parent.CostTimeSelectTab(time);
			}
		},
		/**
		 * 动态设置placeholder提示
		 */
		onSetPlaceText() {
			const type = this.tabMainInfo.extInfo.selectType && this.tabMainInfo.extInfo.selectType[0];
			let text = '';
			switch (type) {
			case EntityTypes.Member:
				text = this.onSetI18n(this.$i18n('common.personnel.label'));
				break;
			case EntityTypes.Account:
				text = this.onSetI18n(this.$i18n('common.company.label'));
				break;
			case EntityTypes.Department:
				text = this.onSetI18n(this.$i18n('message_onlineUser_department'));
				break;
			case EntityTypes.Team:
				text = this.onSetI18n(this.$i18n('org.team.label'));
				break;
			case EntityTypes.Post:
				text = this.onSetI18n(this.$i18n('common.toolbar.post.label'));
				break;
			case EntityTypes.Role:
				text = this.onSetI18n(this.$i18n('org.role.label'));
				break;
			case EntityTypes.Level:
				text = this.onSetI18n(this.$i18n('org.level.label'));
				break;
			case EntityTypes.OrgTeam:
				text = this.onSetI18n(this.$i18n('org.orgTeam.label'));
				break;
			default:
				text = this.$i18n('selectPeople.selectorg.desc.search');
			}
			return text;
		},
		onSetI18n(data) {
			return this.$i18n('selectPeople.input', data);
		},
		/**
		 * 设置列表数据，当type是部门、多维组织、角色时处理id
		*/
		onSetListData(listData) {
			return listData.map(entity => {
				let ids = entity.id.split('|');
				entity.$_showLabel = entity.name;
				if (isSubDeptType(entity) && ids.length > 1) {
					entity.id = ids[0];
					entity.$_excludeChildDepartment = true;
				}
				entity.$_selectKey = getSelectKey(entity);
				return entity;
			})
		},
		/**
		 * 动态设置图标
		*/
		setIconType() {
			const type = this.tabMainInfo.extInfo.selectType && this.tabMainInfo.extInfo.selectType[0];
			let iconType = '';
			switch (type) {
			case EntityTypes.Account:
				iconType = 'cap-icon-alternative-company';
				break;
			case EntityTypes.Department:
				iconType = 'cap-icon-alternative-department';
				break;
			case EntityTypes.Team:
				iconType = 'cap-icon-alternative-group';
				break;
			case EntityTypes.Post:
			case EntityTypes.Level:
			case EntityTypes.Role:
			case EntityTypes.OrgTeam:
				iconType = 'cap-icon-alternative-post';
				break;
			}
			return iconType;
		}
	},
	created() {
	},
	mounted() {
		let { isNeedCheckLevelScope,
			showTeamType,
			showRoleType,
			accessFileSecretLevel,
			includeElements,
			excludeElements,
			returnMemberWithDept,
			memberWithDeptInfo,
			showAllOuterDepartment,
			appParams,
			runMode } = this.orgSelect;

		this.serachParams = { isNeedCheckLevelScope,
			showTeamType,
			showRoleType,
			accessFileSecretLevel,
			includeElements,
			excludeElements,
			returnMemberWithDept,
			memberWithDeptInfo,
			showAllOuterDepartment,
			appParams };
		let orgParams = {runMode};
		this.onSearchData(this.serachParams, orgParams);
	}
};
</script>

<style lang="scss" scoped>
@import "@/css/tool.scss";
.custom-list-tab-wrap {
	width: 100%;
	height: 100%;
}
.condition-wrapper {
	@include conditionWrapper();
}
</style>
